// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var databind = require("databind");
    var firstVisited = 0;
    var tools = require("tools");
    var auditState;
    var leftM;
    var html_createAddress = require("./html_createAddress.html");
    var html_delete = require("./addressManage_delete.html");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(){
    	router.init();
    	inquireAddressInfoPageList();
		initEvent();
    };
    
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".memberGradeAduitList").unbind("click").click(function(){
			router.route("client/memberGradeaudit.html");
		});
    };
    
    //新增地址
    var addAddressInfo = function() {
    	var ii = layer.open({
  		  title: '新增地址',
  		  type: 1,
  		  skin: 'layui-layer-rim', //加上边框
  		  area: ['800px', '250px'], //宽高
  		  content: html_createAddress,
  		  btn: ['新增', '取消'],
  		  yes: function(){
  		  	databind.submitDom($(".createAddressInfo"), function(){
  		  		layer.msg("新增成功！");
  		  		layer.close(ii);
  		  	    inquireAddressInfoPageList();
  		  	});
  		  }
  		});
  		
  		new PCAS("province","city","district");
    };    
	
	//地址详情
	var editorAddressInfo = function(addressID) {
		//获取bean显示
		var ii;
		base.fnAjax({
			action: 'inquireAddressInfoDetail.json',
			async: false,
			oData: {
				addressID: addressID
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					
					base.alert(1, "获取成功！");
					ii = layer.open({
						  title: '编辑地址',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '250px'], //宽高
						  content: html_createAddress,
						  btn: ['编辑', '取消'],
						  yes: function(){
							  updateAddressInfo(data.datas[0]);
							  layer.close(ii);
						  }
						});
					new PCAS("province","city","district");
					databind.fillForm($(".createAddressInfo"), data.datas[0]);
//					$(".createAddressInfo select[name=province]").html('<option value="'+data.datas[0].province+'">'+data.datas[0].province+'</option>');
					$(".createAddressInfo select[name=city]").html('<option value="'+data.datas[0].city+'">'+data.datas[0].city+'</option>');
					$(".createAddressInfo select[name=district]").html('<option value="'+data.datas[0].district+'">'+data.datas[0].district+'</option>');
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//更新地址
	var updateAddressInfo = function(datas) {
		var address = base.parseForm($(".createAddressInfo"));
		address["addressID"] = datas.addressID;
		address["enterpriseID"] = datas.enterpriseID;
		address["createDateTime"] = datas.createDateTime;
		base.fnAjax({
			action: 'updateAddressInfo.json',
			async: false,
			oData: address,
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "编辑成功！");
					inquireAddressInfoPageList();
				} else {
					base.alert(3, data.message);
				}
			}
		});
		
	};
	
	//删除地址
	var deleteAddressInfo = function(addressID) {
		var delete_addressManage_dialog = $("#delete_addressManage_dialog");
    	dialog.showDialog(delete_addressManage_dialog, function() {});
    	$(".footerBtn").on("click", "#sure_delete", function() {
    	base.fnAjax({
            action: "deleteAddressInfo.json",
            oData: {
            	addressID : addressID
            },
            fnSuccess: function(data) {
                if (data.status == 200) {
                	dialog.hideDialog($("#delete_addressManage_dialog"));
                	inquireAddressInfoPageList();
                } else {
                    base.alert(2,"地址删除失败");
                }
            }
        });
    	});
	}
	
    //获取常用地址列表
	var inquireAddressInfoPageList = function(){
        base.fnAjax({
            action: "inquireEnterpriseAddressPageList.json",
            oData: {
            	pageIndex: pageIndex,
                pageSize: pageSize
            },//传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	juicer.register("formatCurrency", tools.formatCurrency);
                	
                	var tpl = $("#addressListTpl").html();
                    var html = juicer(tpl, data);
                    $("#addressListBody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#pagenationAddressManage"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireAddressInfoPageList();
                    });
					
					//删除地址
					$(".delete").unbind("click").click(function(){
						$("#popdialog").html(html_delete);
				        var companyId = [];
				        var $this = $(this).parents("tr.tr_style");
						   companyId.push($this.attr("addressID"));
						   deleteAddressInfo(companyId);
					});
					
					//编辑地址
					$(".editor").unbind("click").click(function(){
						editorAddressInfo($(this).parents("tr").attr("addressID"));
					});
					
					//新增地址
					$(".addressManage").unbind("click").click(function(){
						addAddressInfo();
					});
					
					tools.inquireCurrentOperationList($(".wrapper"));
					
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});